﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PosterMaker
{
    public partial class Form1 : Form
    {
        private Poster poster;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnMakePoster_Click(object sender, EventArgs e)
        {

            //Tmp();
            //return;

            PosterText title = new PosterText
            {
                FontName = "千图马克手写体",
                //FontName = "庞门正道粗书体6.0",
                Content = "喜报",
                //Position = new PointF(1F, 1F),
                Color = Color.FromArgb(242, 223, 181),
                Ratio = 35
            };
            poster.AddText(title, PosterPosition.Center, 7,true);

            PosterText branchName = new PosterText
            {
                FontName = "方正黑体简体",
                Content = "南头支行",
                Color = Color.FromArgb(253, 250, 189),
                Ratio = 10

            };
            poster.AddText(branchName, PosterPosition.Center, 35, true);

            PosterText manager = new PosterText
            {
                FontName = "方正黑体简体",
                Content = "梁文杰",
                Color = Color.FromArgb(253, 250, 189),
                Ratio = 11

            };
            poster.AddText(manager, PosterPosition.Center, 45, true);

            PosterText line = new PosterText
            {
                FontName = "方正黑体简体",
                Content = "--------------------------",
                Color = Color.FromArgb(253, 250, 189),
                Ratio = 11

            };
            poster.AddText(line, PosterPosition.Center, 45, true);

            PosterText item = new PosterText
            {
                FontName = "方正黑体简体",
                Content = "e企快抵新增申请",
                Color = Color.FromArgb(253, 250, 189),
                Ratio = 11

            };
            poster.AddText(item, PosterPosition.Center, 56, false);

            PosterText amount = new PosterText
            {
                FontName = "方正黑体简体",
                Content = "200万",
                Color = Color.FromArgb(253, 250, 189),
                Ratio = 11

            };
            poster.AddText(amount, PosterPosition.Center, 66, true);

           

            pbDocker.Refresh();

            //Tmp();
        }

        /// <summary>
        /// 生成文字图片
        /// </summary>
        /// <param name="text"></param>
        /// <param name="isBold"></param>
        /// <param name="fontSize"></param>
        public Image CreateImage(string text, bool isBold, int fontSize)
        {
            int wid = 400;
            int high = 200;
            Font font;
            if (isBold)
            {
                font = new Font("Arial", fontSize, FontStyle.Bold);

            }
            else
            {
                font = new Font("Arial", fontSize, FontStyle.Regular);

            }
            //绘笔颜色
            SolidBrush brush = new SolidBrush(Color.Black);
            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
            Bitmap image = new Bitmap(wid, high);
            Graphics g = Graphics.FromImage(image);
            SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);//得到文本的宽高
            int width = (int)(sizef.Width + 1);
            int height = (int)(sizef.Height + 1);
            image.Dispose();
            image = new Bitmap(width, height);
            g = Graphics.FromImage(image);
            g.Clear(Color.White);//透明

            RectangleF rect = new RectangleF(0, 0, width, height);
            //绘制图片
            g.DrawString(text, font, brush, rect);
            //释放对象
            g.Dispose();
            return image;
        }

        

        #region 浏览选择底图
        private void btBrowseBackgroundImg_Click(object sender, EventArgs e)
        {
            string backgroundImgPath = SelectBackgroundImg();
            if (backgroundImgPath == "")
                return;

            poster = new Poster(backgroundImgPath);

            //MessageBox.Show(poster.PosterImage.Width.ToString());

            // 如果所选的图片比PictureBox高
            if (poster.BackgroundImage.Height > pbDocker.Height)
            {
                pbDocker.SizeMode = PictureBoxSizeMode.Zoom;
            }
            else
            {
                pbDocker.SizeMode = PictureBoxSizeMode.CenterImage;
            }

            pbDocker.Image = poster.PosterImage;
        }

        private string SelectBackgroundImg()
        {
            string retString = "";
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = "jpg";
            ofd.Filter = "所有图片文件|*.jpg;*.jpeg;*.gif;*.bmp;*.png|JPEG|*.jpg;*.jpeg|Gif|*.gif|Bmp|*.bmp|PNG|*.png";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtBackgroudImgPath.Text = ofd.FileName;
                retString = ofd.FileName;
            }
            return retString;
        }
        #endregion

        #region 浏览选择源Excel
        private void btnBrowseSourceExcel_Click(object sender, EventArgs e)
        {
            string sourceExcelPath = SelectSourceExcel();
            if (sourceExcelPath == "")
                return;
        }

        private string SelectSourceExcel()
        {
            string retString = "";
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.DefaultExt = "xls";
            ofd.Filter = "Excel文件|*.xls;*.xlsx";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtSourceExcelPath.Text = ofd.FileName;
                retString = ofd.FileName;
            }
            return retString;
        }
        #endregion
    }
}
